package com.zimmur.service.impl.goods;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.zimmur.common.ConstData;
import com.zimmur.dao.goods.IGoodsTypeDao;
import com.zimmur.model.goods.GoodsParameter;
import com.zimmur.model.goods.GoodsType;
import com.zimmur.model.goods.view.GoodsTypeView;
import com.zimmur.service.RedisCacheUtil;
import com.zimmur.service.goods.IGoodsTypeService;

@Service("goodsTypeServiceImpl") 
public class GoodsTypeServiceImpl implements IGoodsTypeService{
	private static Logger log=LoggerFactory.getLogger(GoodsTypeServiceImpl.class);
    @Resource  
    private IGoodsTypeDao goodsTypeDao;
	@Resource
	private RedisCacheUtil<GoodsType> stringRedisDao;
	
	@Resource
	private RedisCacheUtil<GoodsTypeView> viewRedisDao;

	private final static String REDIS_KEY ="popolook_goods_type";
    
	private static Object syncLock = new Object();
	
	@Override
	public List<GoodsType> selectByTypeFlagGoodsType(Short typeFlag) {
		List<GoodsType> goodsTypeList=null;
		String key = REDIS_KEY + "_" + typeFlag + "_selectByTypeFlagGoodsType";
		try {
			//获取 商品类型列表
			boolean bRedis = false;
			try {
				bRedis = stringRedisDao.exists(key);
			} catch (Exception e) {
				log.error(e.getMessage());
			}
			if(bRedis){
				//从缓存中读取
				goodsTypeList = stringRedisDao.getCacheList(key);
			}else{
				synchronized (syncLock) {
					try {
						bRedis = stringRedisDao.exists(key);
					} catch (Exception e) {
						log.error(e.getMessage());
					}
					if(bRedis){
						//从缓存中读取
						goodsTypeList = stringRedisDao.getCacheList(key);
					}else{
						/*缓存中不存在，创建后存入*/
						//从数据库读取
						goodsTypeList = goodsTypeDao.selectByTypeFlagGoodsType(typeFlag);
						try {
							//存入缓存
							stringRedisDao.setCacheList(key,goodsTypeList);
							stringRedisDao.setTimeout(key, ConstData.REDIS_KEY_TIMEOUT);
						} catch (Exception e) {
							log.error(e.getMessage());
						}
					}
				}
			}
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return goodsTypeList;
	}

	@Override
	public List<GoodsType> selectByParentTypeIdGoodsType(GoodsParameter goodsParameter) {
		return goodsTypeDao.selectByParentTypeIdGoodsType(goodsParameter);
	}

	@Override
	public GoodsType selectByPrimaryId(Integer typeId) {
		return goodsTypeDao.selectByPrimaryKey(typeId);
	}

	@Override
	public GoodsType selectByTypeIdGoodsType(Integer typeId) {
		return goodsTypeDao.selectByTypeIdGoodsType(typeId);
	}

	@Override
	public List<GoodsTypeView> selectAllType() {
		List<GoodsTypeView> typeViews=new ArrayList<>();
		
		String key = REDIS_KEY + "_" + "AllType" + "_selectAllType";
		try {
			//是否已存在
			boolean bRedis = false;
			try {
				bRedis = viewRedisDao.exists(key);
			} catch (Exception e) {
				log.error(e.getMessage());
			}
			if(bRedis){
				//从缓存中读取
				typeViews = viewRedisDao.getCacheList(key);
			}else{
				synchronized (syncLock) {
					try {
						bRedis = viewRedisDao.exists(key);
					} catch (Exception e) {
						log.error(e.getMessage());
					}
					if(bRedis){
						//从缓存中读取
						typeViews = viewRedisDao.getCacheList(key);
					}else{
						//缓存中不存在，创建后存入
						//从数据库读取
						List<GoodsType> types=selectByTypeFlagGoodsType((short)1);
						GoodsParameter goodsParameter=new GoodsParameter();
						for(GoodsType type:types){
							
							goodsParameter.setTypeId(type.getTypeId());
							List<GoodsType> childType=selectByParentTypeIdGoodsType(goodsParameter);
							
							GoodsTypeView goodsTypeView=new GoodsTypeView();
							goodsTypeView.setGoodsType(type);
							goodsTypeView.setChildrenType(childType);
							typeViews.add(goodsTypeView);
						}
						try {
							//存入缓存
							stringRedisDao.setCacheList(key,typeViews);
							stringRedisDao.setTimeout(key, ConstData.REDIS_KEY_TIMEOUT);
						} catch (Exception e) {
							log.error(e.getMessage());
						}
					}
				}
			}
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return typeViews;
	}

	@Override
	public List<GoodsType> selectAllHomeType() {
		return goodsTypeDao.selectAllHomeType();
	}

	@Override
	public List<GoodsTypeView> selectAllTypeByFlag(Short typeFlag) {
		List<GoodsTypeView> typeViews=new ArrayList<>();
		List<GoodsType> types= goodsTypeDao.selectAllTypeByFlag(typeFlag);
		GoodsParameter goodsParameter=new GoodsParameter();
		for(GoodsType type:types){
			goodsParameter.setTypeId(type.getTypeId());
			List<GoodsType> childType=selectByParentTypeIdGoodsType(goodsParameter);
			GoodsTypeView goodsTypeView=new GoodsTypeView();
			goodsTypeView.setGoodsType(type);
			goodsTypeView.setChildrenType(childType);
			typeViews.add(goodsTypeView);
		}
		return typeViews;
	}

	@Override
	public List<GoodsType> selectByParentTypeId(Integer parentTypeId) {
		return goodsTypeDao.selectByParentTypeId(parentTypeId);
	}

}
